package cn.nnxy.zentao.controller;

import cn.nnxy.zentao.domain.ZTest;
import cn.nnxy.zentao.service.TestService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/bugTest")
public class TestController {
    @Autowired
    private TestService testService;

    @GetMapping("test")
    public String testListUI() {
        return "";
    }

    @ResponseBody
    @PostMapping("test")
    public Map<String, Object> testList(){
        Integer pageSize = 10;
        return null;
    }

    //新建计划
    @GetMapping("testAdd")
    public String testAddUI(){
        return "";
    }

    @PostMapping("testAdd")
    public String testAdd(ZTest test, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("test",test);
            model.addAttribute("msg", result.getAllErrors().get(0).getDefaultMessage());
            return "";
        }
        int temp = testService.saveTest(test);
        if (temp == 1){
            model.addAttribute("msg", "添加成功！");
            return "redirect:";
        }else {
            model.addAttribute("msg", "添加失败！");
            return "";
        }

    }

    //删除计划
    @ResponseBody
    @PostMapping("testDel")
    public boolean testDel(Integer ids){
        if (null == ids ){
            return false;
        }
        return testService.deleteById(ids) > 0;
    }

    //编辑计划
    @GetMapping("testEdit")
    public String testEditUI(Integer testId, Model model){
        ZTest test = testService.seletById(testId);
        model.addAttribute("test", test);
        return "";
    }

    @PostMapping("testEdit")
    public String testEdit(ZTest test, BindingResult result, Model model){
        if (result.hasErrors()){
            model.addAttribute("test", test);
            model.addAttribute("msg", result.getAllErrors());
            return "";
        }
        int temp = testService.updateById(test);
        if (temp == 1){
            model.addAttribute("msg", "修改成功。");
            return "redirect:";
        }else {
            model.addAttribute("msg", "修改失败。");
            return "";
        }

    }
}
